package com.notepad;

/**
 * Created by jiamengyu on 1/6/2015.
 */
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.notepad.common.ProtocolElement;
import com.notepad.common.RequestData;
import com.notepad.common.ResponseData;
import com.notepad.domain.Notepad;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MyTest extends Activity implements View.OnClickListener {
    private static final String URL = "http://tibby.aliapp.com/note";
    private static final String TAG = "responseData";
    private Button mAddButton;
    private Button mDeleteButton;
    private Button mFindButton;
    private Button mUpdateButton;
    private Button mTestButton;


    private RequestQueue mRequestQueue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_activity);
        mRequestQueue = Volley.newRequestQueue(this);
        initViewId();
        setViewListener();
    }

    private void initViewId(){
        this.mAddButton = (Button) this.findViewById(R.id.button);
        this.mDeleteButton = (Button) this.findViewById(R.id.delete_button);
        this.mFindButton = (Button) this.findViewById(R.id.find_button);
        this.mUpdateButton = (Button) this.findViewById(R.id.update_button);
        this.mTestButton = (Button) this.findViewById(R.id.testbutton);

    }

    private void setViewListener(){
        this.mAddButton.setOnClickListener(this);
        this.mDeleteButton.setOnClickListener(this);
        this.mFindButton.setOnClickListener(this);
        this.mUpdateButton.setOnClickListener(this);
        this.mTestButton.setOnClickListener(this);

    }

    private void addNotepad(){
        RequestData requestData = new RequestData();
        requestData.setCommand(0);
        Notepad notepad = new Notepad();
        //notepad.setNoteId(System.currentTimeMillis());
        notepad.setTitle("hello note");
        notepad.setContent("11this is a note test!");
        notepad.setTime("2015-01-04");
        requestData.getExtra().put(ProtocolElement.NOTEPAD, JSON.toJSONString(notepad));
        Map<String,String> map = new HashMap<String,String>();
        map.put(ProtocolElement.REQUEST_DATA,JSON.toJSONString(requestData));
        StringRequest request = new StringRequest(URL,map,new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Toast.makeText(MyTest.this,"success",Toast.LENGTH_SHORT).show();
                Log.d(TAG,response.replace("\\",""));
            }
        },new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MyTest.this,"request fail",Toast.LENGTH_SHORT).show();
                Log.d(TAG,error.getMessage());
            }
        });
        mRequestQueue.add(request);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                addNotepad();
                break;
            case R.id.delete_button:
                deleteButton();
                break;
            case R.id.update_button:
                UpdateNotepad();
                break;
            case R.id.find_button:
                findAllNotepad();
            case R.id.testbutton:
             //   test();
            default:
                break;
        }
    }

    /*private void test() {
        String str = "";
        ResponseData responseData = JSON.parseObject(str,ResponseData.class);
        String result = responseData.getExtra().get(ProtocolElement.NOTEPAD_LIST).toString();
        List<Notepad> list = JSON.parseArray(result,Notepad.class);
        for(Notepad n:list){
            System.out.println(n);
        }
    }*/

    private void findAllNotepad() {
        RequestData requestData = new RequestData();
        requestData.setCommand(3);
        Map<String,String> map = new HashMap<>();
        map.put(ProtocolElement.REQUEST_DATA,JSON.toJSONString(requestData));
        StringRequest findRequest = new StringRequest(URL,map,new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                ResponseData responseData = JSON.parseObject(response,ResponseData.class);
                String result = responseData.getExtra().get(ProtocolElement.NOTEPAD_LIST).toString();
                List<Notepad> list = JSON.parseArray(result,Notepad.class);
                for(Notepad n:list){
                    System.out.println(n);
                }


                Toast.makeText(MyTest.this,"find success",Toast.LENGTH_LONG).show();
                Log.d(TAG,response.replace("\\",""));



            }
        },new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MyTest.this,error.getMessage(),Toast.LENGTH_LONG).show();
            }
        });

        mRequestQueue.add(findRequest);
    }

    private void UpdateNotepad() {
        RequestData requestData = new RequestData();
        requestData.setCommand(2);
        Notepad notepad = new Notepad();
        notepad.setNoteId(Long.parseLong("1420359998906"));
        notepad.setTitle("1111hello note1111");
        notepad.setContent("1111this is a note test!1111");
        notepad.setTime("11111");
        requestData.getExtra().put(ProtocolElement.NOTEPAD,JSON.toJSONString(notepad));
        Map<String,String> map = new HashMap<>();
        map.put(ProtocolElement.REQUEST_DATA,JSON.toJSONString(requestData));
        StringRequest updateRequest = new StringRequest(URL,map, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Toast.makeText(MyTest.this,"update success",Toast.LENGTH_LONG).show();
                Log.d(TAG,response.replace("\\",""));
            }
        },new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MyTest.this,error.getMessage(),Toast.LENGTH_LONG).show();
            }
        });
        mRequestQueue.add(updateRequest);
    }

    private void deleteButton() {
        RequestData requestData = new RequestData();
        requestData.setCommand(1);
        Long id = Long.parseLong("1420364974759");
        requestData.getExtra().put(ProtocolElement.NOTE_ID, JSON.toJSONString(id));
        Map<String,String> map = new HashMap<>();
        map.put(ProtocolElement.REQUEST_DATA,JSON.toJSONString(requestData));
        StringRequest request = new StringRequest(URL,map,new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Toast.makeText(MyTest.this,"delete success",Toast.LENGTH_LONG).show();
                Log.d(TAG,response.replace("\\",""));
            }
        },new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MyTest.this,error.getMessage(),Toast.LENGTH_LONG).show();
            }
        });

        mRequestQueue.add(request);
    }
}
